import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProcessManagement extends JFrame {

	private JPanel contentPane;
	private JTextField textProcess;
	private JTextField textPrioroty;
	private JTextArea textShow;
	private ProcessQueue processQueue;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProcessManagement frame = new ProcessManagement();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProcessManagement() {
		setTitle(" Process Management");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 441);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblProcess = new JLabel("Process:");
		lblProcess.setBounds(9, 13, 64, 18);
		contentPane.add(lblProcess);

		textProcess = new JTextField();
		textProcess.setBounds(82, 10, 86, 24);
		contentPane.add(textProcess);
		textProcess.setColumns(10);

		JLabel lblPrioroty = new JLabel("Prioroty:");
		lblPrioroty.setBounds(177, 13, 78, 18);
		contentPane.add(lblPrioroty);

		textPrioroty = new JTextField();
		textPrioroty.setColumns(10);
		textPrioroty.setBounds(264, 10, 86, 24);
		contentPane.add(textPrioroty);

		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnAddCall();
			}
		});
		btnAdd.setBounds(359, 9, 61, 27);
		contentPane.add(btnAdd);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(9, 44, 411, 337);
		contentPane.add(scrollPane);

		textShow = new JTextArea();
		textShow.setEditable(false);
		scrollPane.setViewportView(textShow);

		init();
	}

	protected void btnAddCall() {
		if (textProcess.getText().length() < 1) {
			showMassage("Input a process!");
			return;
		}
		if (textPrioroty.getText().length() < 1) {
			showMassage("Input a Prioroty!");
			return;
		}
		try {
			int prioroty = Integer.parseInt(textPrioroty.getText());
			this.processQueue.offer(textProcess.getText(), prioroty);
			showProcess();
		} catch (Exception e) {
			showMassage("Error:" + e.getMessage());
			return;
		}
	}

	private void init() {
		this.processQueue = new ProcessQueue();
		showProcess();
	}

	private void showProcess() {
		String pString = processQueue.getString();
		textShow.setText(pString);
	}

	private void showMassage(String message) {
		String pString = message + "\n";
		pString = pString + processQueue.getString();
		textShow.setText(pString);
	}
}
